package v1

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"next-vue-backend/model"
	"next-vue-backend/utils/errmsg"
	"strconv"
)

// AddComment 新增评论
func AddComment(c *gin.Context) {
	addCommentForm := model.AddCommentForm{}
	_ = c.ShouldBindJSON(&addCommentForm)
	comment := model.Comment{
		UserId:    addCommentForm.UserId,
		ArticleId: addCommentForm.ArticleId,
		Content:   addCommentForm.Content,
	}

	code = model.AddComment(&comment)
	data, code := model.GetSingleComment(comment.CommentId)
	c.JSON(http.StatusOK, gin.H{
		"status": code,
		"data": gin.H{
			"username": data.User.Username,
			"avatar":   data.User.Avatar,
			"content":  data.Content,
			"parentId": data.ParentId,
		},
		"message": errmsg.GetErrMsg(code),
	})
}

// GetSingleComment 获取单条评论
func GetSingleComment(c *gin.Context) {
	id, _ := strconv.Atoi(c.Param("comment_id"))

	data, code := model.GetSingleComment(id)
	c.JSON(http.StatusOK, gin.H{
		"status":  code,
		"data":    data,
		"message": errmsg.GetErrMsg(code),
	})
}

// DeleteComment 删除评论
func DeleteComment(c *gin.Context) {
	id, _ := strconv.Atoi(c.Param("id"))

	code := model.DeleteComment(uint(id))
	c.JSON(http.StatusOK, gin.H{
		"status":  code,
		"message": errmsg.GetErrMsg(code),
	})
}

// GetCommentCount 获取评论数量
func GetCommentCount(c *gin.Context) {
	id, _ := strconv.Atoi(c.Param("id"))
	total := model.GetCommentCount(id)
	c.JSON(http.StatusOK, gin.H{
		"total": total,
	})
}

// GetCommentList 后台查询评论列表
func GetCommentList(c *gin.Context) {
	pageSize, _ := strconv.Atoi(c.Query("pagesize"))
	pageNum, _ := strconv.Atoi(c.Query("pagenum"))

	switch {
	case pageSize >= 100:
		pageSize = 100
	case pageSize <= 0:
		pageSize = 10
	}

	if pageNum == 0 {
		pageNum = 1
	}

	data, total, code := model.GetCommentList(pageSize, pageNum)
	c.JSON(http.StatusOK, gin.H{
		"status":  code,
		"data":    data,
		"total":   total,
		"message": errmsg.GetErrMsg(code),
	})
}

// CommentTreeVO 评论树
type CommentTreeVO struct {
	CommentId int             `json:"commentId"`
	Content   string          `json:"content"`
	Username  string          `json:"username"`
	Avatar    string          `json:"avatar"`
	CreatedAt string          `json:"createdAt"`
	ParentId  int             `json:"parentId"`
	Children  []CommentTreeVO `json:"children"`
}

// QueryCommentList 后台查询评论列表
func QueryCommentList(c *gin.Context) {
	articleId, _ := strconv.Atoi(c.Query("article_id"))
	//data, code := model.QueryCommentList(articleId)
	data, code := model.QueryCommentList(articleId)
	// var commentList []CommentTreeVO
	if code == errmsg.SSUCCESS && len(data) > 0 {
		//commentList = findChildren(0, data)
	}

	c.JSON(http.StatusOK, gin.H{
		"status":  code,
		"data":    data,
		"message": errmsg.GetErrMsg(code),
	})
}

func findChildren(parentId int, list []model.Comment) []CommentTreeVO {
	res := make([]CommentTreeVO, 0)
	for _, comment := range list {
		if comment.ParentId == parentId {
			user := comment.User
			commentTreeVO := CommentTreeVO{
				CommentId: comment.CommentId,
				Content:   comment.Content,
				Username:  user.Username,
				Avatar:    user.Avatar,
				CreatedAt: comment.CreatedAt.Format("2006-01-02 15:04"),
			}
			commentTreeVO.Children = findChildren(comment.CommentId, list)
			res = append(res, commentTreeVO)
		}
	}
	return res
}

// GetCommentListFront 获取展开文章的评论列表
func GetCommentListFront(c *gin.Context) {
	id, _ := strconv.Atoi(c.Param("id"))
	pageSize, _ := strconv.Atoi(c.Query("pagesize"))
	pageNum, _ := strconv.Atoi(c.Query("pagenum"))

	switch {
	case pageSize >= 100:
		pageSize = 100
	case pageSize <= 0:
		pageSize = 10
	}

	if pageNum == 0 {
		pageNum = 1
	}

	data, total, code := model.GetCommentListFront(id, pageSize, pageNum)

	c.JSON(http.StatusOK, gin.H{
		"status":  code,
		"data":    data,
		"total":   total,
		"message": errmsg.GetErrMsg(code),
	})
}

// GetArticleCommentList 按文章查询评论列表
func GetArticleCommentList(c *gin.Context) {
	var commentTrees []model.CommentTree
	commentId, _ := strconv.Atoi(c.Query("article_id"))
	commentTrees = model.GetCommentListByArticleId(commentId)
	c.JSON(http.StatusOK, gin.H{
		"status": 200,
		"data":   commentTrees,
	})
}

// CheckedComment 通过评论
func CheckedComment(c *gin.Context) {
	var data model.Comment
	_ = c.ShouldBindJSON(&data)
	id, _ := strconv.Atoi(c.Param("id"))

	code = model.CheckedComment(id, &data)
	c.JSON(http.StatusOK, gin.H{
		"status":  code,
		"message": errmsg.GetErrMsg(code),
	})
}

// 关闭评论
func CloseComment(c *gin.Context) {
	var data model.Comment
	_ = c.ShouldBindJSON(&data)
	id, _ := strconv.Atoi(c.Param("id"))

	code = model.CloseComment(id, &data)
	c.JSON(http.StatusOK, gin.H{
		"status":  code,
		"message": errmsg.GetErrMsg(code),
	})
}
